package com.winit.openapi.wh.builder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.query.Searchable;
import com.winit.common.query.Sort;
import com.winit.common.query.Sort.Direction;
import com.winit.oms.spi.inbound.vo.InboundMerchandiseDetailsVo;
import com.winit.openapi.constants.ApiConstant;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.util.ValidateUtil;

/**
 * 海外仓入库单构造器 商品信息
 * 
 * @version <pre>
 * Author	    Version		Date		Changes
 * ming.chen 	1.0  		2017-3-21 	Created
 * 
 * </pre>
 * @since 1.
 */
public class InboundMerchandiseBuilder {

    /**
     * 构建商品信息对象
     * 
     * @param list
     * @return
     */
    public static List<Map<String, Object>> buildReturnMerchandiseList(List<InboundMerchandiseDetailsVo> list) {
        List<Map<String, Object>> mList = new ArrayList<Map<String, Object>>();

        for (InboundMerchandiseDetailsVo vo : list) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("inboundMerchandiseId", vo.getInboundMerchandiseId());
            map.put("merchandiseSerno", vo.getMerchandiseSerno());
            map.put("merchandiseCode", vo.getMerchandiseCode());
            map.put("nameCn", vo.getNameCn());
            map.put("specification", vo.getSpecification());
            map.put("estimateQty", vo.getEstimateQty());
            map.put("inspectionItemQty", vo.getInspectionItemQty());
            map.put("sellerLength", vo.getSellerLength());
            map.put("sellerWidth", vo.getSellerWidth());
            map.put("sellerHeight", vo.getSellerHeight());
            map.put("sellerWeight", vo.getSellerWeight());
            map.put("sellerVolume", vo.getSellerVolume());
            map.put("isSingleManagement", vo.getIsSingleManagement());
            map.put("length", vo.getLength());
            map.put("width", vo.getWidth());
            map.put("height", vo.getHeight());
            map.put("weight", vo.getWeight());
            map.put("volume", vo.getVolume());
            map.put("shelveQty", vo.getShelveQty());
            map.put("actualQuantity", vo.getActualQty());
            map.put("deductionQty", vo.getDeductionQty());
            mList.add(map);
        }
        return mList;
    }

    /**
     * 构建分页参数
     * 
     * @param json
     * @return
     */
    protected Searchable buildSearchablePageParams(JSONObject json) {
        int pageNo = ApiConstant.PAGE_NO_DEFAULT;
        int pageSize = ApiConstant.PAGE_SIZE_DEFAULT;
        // pageParams
        if (isExistValue(json, "pageParams")) {
            JSONObject pageParams = json.getJSONObject("pageParams");

            if (ValidateUtil.isIntNumber(pageParams.getString("pageNo"))) {
                pageNo = pageParams.getInteger("pageNo");
                if (pageNo <= 0) {
                    throw new ApiException(ErrorCode.FIELD_ERROR, "pageNo");
                }
            } else if (isExistValue(pageParams, "pageNo") && !ValidateUtil.isIntNumber(pageParams.getString("pageNo"))) {
                throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "pageNo");
            }

            if (ValidateUtil.isIntNumber(pageParams.getString("pageSize"))) {
                pageSize = pageParams.getInteger("pageSize");
                if (pageSize <= 0) {
                    throw new ApiException(ErrorCode.FIELD_ERROR, "pageSize");
                }
            } else if (isExistValue(pageParams, "pageSize")
                       && !ValidateUtil.isIntNumber(pageParams.getString("pageSize"))) {
                throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "pageSize");
            }

        }
        Searchable search = Searchable.newSearchable();
        search.setPage(pageNo, pageSize);
        return search;
    }

    // 判断非必填字段是否有值
    public boolean isExistValue(JSONObject jsonObject, String key) {
        if (jsonObject.containsKey(key)) {
            return !"".equals(jsonObject.getString(key).trim());
        }
        return false;
    }
}
